# -*- py-indent-offset: 4 -*- 
# -*- tab-width: 8 -*- 
# -*- indent-tabs-mode: nil -*-

import CmdNo,string
from etgui import *

from PageLayout import *
from ProjAttsGeneral import *
from ProjAttsBuild import *
from ProjAttsParser import *
from ProjAttsVCS import *
from ProjAttsFileTypes import *
from ProjPlatformSpecific import MakePagePlatformSpecific
from ListEditor import ListEditor
class SetForAllButton:
	"""Button for generating something e.g. GenIncludeMacro"""
	def __init__(self,model):
		self.model=model
	def __call__(self):
		self.model.SetValue("PSEUDO.SetForAll",1)
class ForallEnabler(FieldEnabler):
	def IsEnabled(self,value):
		return value>0

class ModelListEditor(ListEditor):
	"""Items beginning with - are not deletable and shown in Italic."""
	def __init__(self,model):
		self.items=None
		self.model=model
		model.AddModelObserver(self.Observe,"")
		#model.AddModelObserver(TraceModelObserver("ModelListEditor"))
		ListEditor.__init__(self)
	def Observe(self,model,path):
		#print "ModelListEditor.Observe",(model,path)
		if path=="":
			self.ObserveReload(model,path)
		elif path=="PSEUDO.Goto":
			self.ObserveGoto(model,path)
	def ObserveGoto(self,model,path):
		#print "ObserveGoto",(model,path)
		i=self.model.GetValue("PSEUDO.Goto")
		self.Select(i)
		# set the title
		gMultiProjDialog.SetName("Attributes of %s"%self.model.GetValue("ProjectName"))
	def ObserveReload(self,model,path):
		try:
			items= map(None,
				self.model.GetValue("PSEUDO.ALL.ProjectName"),
				self.model.GetValue("PSEUDO.ALL.Editable")
			)
		except:
			items=None
		if self.items!=items:
			self.SetItems(items)
		if not self._GetCollView().IsOpen():
			self.Select(0) # if closed goto the first page
	def DoMakeItem(self,value):
		name,status=value
		if not status:
			return TextItem(name)
		return TextItem(
			name,
			font=Font(
				gSysFont.Fid(),
				gSysFont.Size(),
				eFaceBold
			)
		)
	def CanChangeSelection(self,i):
		return self.model.GetValue("PSEUDO.CanApply")
	def DoSelect(self,i):
		self.model.SetValue("PSEUDO.Goto",i)
	DoEdit=None
	DoDelete=None
	DoSwap=None
	def DoNext(self,i):
		pass
	DoPrev=DoNext
 
 
def MakeDialog(model,new_proj=0,multi=0):
	panel=PotGuiPanel(gPoint6,eVObjVExpand|eVObjHExpand)
	l=0
	if new_proj:
		panel.AddPage(MakeHeader("General","Specify new project settings",
									MakePageGeneralNew(model)),l+0,"General")
	else:
		panel.AddPage(MakeHeader("General","Specify general project settings",
									MakePageGeneral(model)),l+0,"General")
	panel.AddPage(MakeHeader("Advanced","Specify advanced project settings",
								MakePageGeneralAdvanced(model)),l+1,"Advanced")
	panel.AddPage(MakeHeader("Build Options","Specify make support options",
								MakePageBuild(model)),l+0,"Build Options")
	panel.AddPage(MakeHeader("Directives","Specify platform specific Directives",
								MakePagePlatformSpecific(model)),l+1,"Directives")
	panel.AddPage(MakeHeader("Project Targets","Specify project targets",
								MakePageBuildProjectTargets(model)),l+1,"Project Targets")
	panel.AddPage(MakeHeader("Build Structure","Specify project build structure",
								MakePageBuildBuildStructure(model)),l+1,"Build Structure")
	box=MakeHeader("Advanced","Specify advanced project make support options",
								MakePageBuildAdvanced(model))
	panel.AddPage(box,l+1,"Advanced")
	box.SetHelpID("advancedbo")
	panel.AddPage(MakeHeader("Parser","Specify parser settings",
								MakePageParser(model)),l+0,"Parser")
	panel.AddPage(MakeHeader("Version Control System","Specify VCS settings",
								MakePageVCS(model)),l+0,"Version Control System")
	if new_proj:
		panel.AddPage(MakeHeader("File Types","Configure project file types",
									MakePageFileTypesNew(model)),l+0,"File Types")
	else:
		panel.AddPage(MakeHeader("File Types","Configure project file types",
									MakePageFileTypes(model)),l+0,"File Types")
	panel.Update()
	buttons=(
			ActionButton(CmdNo.cIdOk,"OK",1),
			ActionButton(CmdNo.cIdCancel,"Cancel"),
	)
	if multi:
		ed=ModelListEditor(model)
		ed._GetCollView().SetHelpID("projlist")
		setforall=ActionButton("Set for All",action=SetForAllButton(model))
		ForallEnabler(model,"PSEUDO.SetForAll",[setforall])
		buttons=buttons+(setforall,)+tuple(ed.MakeButtons())
		panel=HBox(
			align=eVObjVExpand|eVObjHExpand,
			items=[
				panel,
				Scroller(ed._GetCollView(),Point(100,0))
			]
		)
	dialog=PotGuiDialog(
		model,
		apply(MakeDialogInner,(panel,)+buttons),
		"Project Attributes",
	)
	return dialog
 
def CheckLanguageTabPages(model):
	if model.GetValue("IsJavaProject"):
		SwitchToLanguageTabPages("java")
	else:
		SwitchToLanguageTabPages("cpp")

def GetProjModel(what):
	model=GetNamedModel(what)
	if IsNULL(model):
		model=PotGuiModel()
		modelio=ModelIO(model)
		modelio.Load()
	return model


gModel=gProjDialog=None
def InitDialog():
	global gModel,gProjDialog
	if not gProjDialog:
		gModel=PotGuiModelDecorator(PotGuiModel())
		gProjDialog=MakeDialog(gModel)
def Show():
	InitDialog()
	gModel.SetModel(GetProjModel("ProjAtts"))
	CheckLanguageTabPages(gModel)
	gProjDialog.SetName("Attributes of %s"%gModel.GetValue("ProjectName"))
	return gProjDialog.ShowUnderMouse()==CmdNo.cIdOk

gNewModel=gNewProjDialog=None
def InitNewDialog():
	global gNewModel,gNewProjDialog
	if not gNewProjDialog:
		gNewModel=PotGuiModelDecorator(PotGuiModel())
		gNewProjDialog=MakeDialog(gNewModel,1)
def ShowNew():
	InitNewDialog()
	gNewModel.SetModel(GetProjModel("ProjAtts"))
	CheckLanguageTabPages(gNewModel)
	gNewProjDialog.SetName("Attributes of New Project")
	return gNewProjDialog.ShowUnderMouse()==CmdNo.cIdOk

gMultiModel=gMultiProjDialog=None
def InitMultiDialog():
	global gMultiModel,gMultiProjDialog
	if not gMultiProjDialog:
		PotGuiVObj_BeginAddMarkButtons()
		gMultiModel=PotGuiModelDecorator(PotGuiModel())
		gMultiProjDialog=MakeDialog(gMultiModel,multi=1)
		PotGuiVObj_EndAddMarkButtons()

def ShowMulti():
	InitMultiDialog()
	gMultiModel.SetModel(GetProjModel("ProjAtts"))
	CheckLanguageTabPages(gMultiModel)
	return gMultiProjDialog.ShowUnderMouse()==CmdNo.cIdOk
